﻿using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ini编辑器CS
{
    public partial class Form1 : Form
    {
        string[] imgpath;
        const int Normal = 0;
        public Form1()
        {
            InitializeComponent();
            Global.f1 = this;
            imgpath = new string[1];
        }

        private void 加载MODToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ZipFile zip = new ZipFile(openFileDialog1.FileName);
                string extpath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                string extpath2 = extpath;
                int i = 0;
                while (Directory.Exists(extpath) == true)//如果不存在就创建file文件夹
                {
                    extpath = extpath2+i;
                    i++;
                }
                zip.ExtractAll(extpath, ExtractExistingFileAction.OverwriteSilently);
                zip.Dispose();
                Global.mod = new MOD(openFileDialog1.FileName, extpath + "\\");
                imgpath[Normal] = Global.mod.cardPile.img[0].path;
                Image img = Image.FromFile(imgpath[Normal]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox1.Image = bmp;
                保存MODToolStripMenuItem.Enabled = true;
                另存为ToolStripMenuItem.Enabled = true;
                pictureBox1.Visible = true;
                button1.Visible = true;
                button2.Visible = true;
                button3.Visible = true;
                button4.Visible = true;
                listView1.Visible = true;
                textBox1.Visible = true;
            }

        }

        private void 保存MODToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (imgpath[Normal] == null)
            {
                MessageBox.Show("请选择图片！");
                return;
            }
            if (Global.mod.filepath==null)
            {
                saveFileDialog1.FileName = Global.mod.name;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Global.mod.filepath = saveFileDialog1.FileName;
                }
                else return;
            }
            Global.mod.Save();
            ZipFile zip = new ZipFile();
            zip.AddDirectory(Global.mod.foldpath);
            zip.Save(Global.mod.filepath);
        }

        private void 新建MODToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
            if (Global.mod != null)
            {
                pictureBox1.Image = null;
                textBox1.Text = Global.mod.cardPile.Name;
                listView1.Items.Clear();
                保存MODToolStripMenuItem.Enabled = true;
                另存为ToolStripMenuItem.Enabled = true;
                pictureBox1.Visible = true;
                button1.Visible = true;
                button2.Visible = true;
                button3.Visible = true;
                button4.Visible = true;
                listView1.Visible = true;
                textBox1.Visible = true;
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (imgpath[Normal] == null)
            {
                MessageBox.Show("请选择图片！");
                return;
            }
            saveFileDialog1.FileName = Global.mod.name;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ZipFile zip = new ZipFile();
                zip.AddDirectory(Global.mod.foldpath);
                zip.Save(saveFileDialog1.FileName);
            }
        }
        public void AddCardItem(Card card)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = card.ID;
            lvi.SubItems.Add(card.Name);
            lvi.SubItems.Add(card.Type);
            this.listView1.Items.Add(lvi);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            Form2 f2;
            Card card = Global.mod.cardPile.CardList.Find(o => o.ID == listView1.FocusedItem.SubItems[0].Text);
            if (card != null)
            {
                f2 = new Form2(this, listView1.FocusedItem, card);
                f2.ShowDialog();
                //UpdateListView();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2;
            Card card = new Card(Global.mod.cardPile);
            f2 = new Form2(this, listView1.FocusedItem, card);
            f2.ShowDialog();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.FocusedItem != null)
            {
                string text = listView1.FocusedItem.SubItems[0].Text;
                Card card = Global.mod.cardPile.CardList.Find(o => o.ID == text);
                listView1.Items.Remove(listView1.FocusedItem);
                Global.mod.cardPile.CardList.Remove(card);

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.FocusedItem != null)
            {
                Card card = Global.mod.cardPile.CardList.Find(o => o.ID == listView1.FocusedItem.SubItems[0].Text);
                if (card != null)
                {
                    Global.mod.cardPile.CardList.Add(card);
                    AddCardItem(card);
                }
            }
        }
        public void UpdateListView()
        {
            listView1.BeginUpdate();
            listView1.Items.Clear();
            for (int i = 0; i < Global.mod.cardPile.CardList.Count; i++)
            {
                AddCardItem(Global.mod.cardPile.CardList[i]);
            }
            listView1.EndUpdate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(Global.mod);
            f3.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                imgpath[Normal] = openFileDialog2.FileName;
                Image img = Image.FromFile(imgpath[Normal]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox1.Image = bmp;
                Global.mod.cardPile.img[Normal].path = imgpath[Normal];
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(Global.mod!=null)
            Global.mod.cardPile.Name = textBox1.Text;
        }
    }
}
